package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.GetFileContentUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysFiles;
import com.ruoyi.system.domain.vo.FileVo;
import com.ruoyi.system.mapper.SysFileMapper;
import com.ruoyi.system.service.ISysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class ISysFileServiceImpl implements ISysFileService {
    private final SysFileMapper sysFileMapper;

    @Autowired
    public ISysFileServiceImpl(SysFileMapper sysFileMapper) {
        this.sysFileMapper = sysFileMapper;
    }

    @Override
    public void createFile(FileVo fileVo, MultipartFile multipartFile) {
        Integer projId = fileVo.getProjId();
        if (multipartFile.isEmpty()) {
            throw new CustomException("文件不能为空");
        }
        String originalFilename = multipartFile.getOriginalFilename();
        if (StringUtils.isEmpty(originalFilename)) {
            throw new CustomException("文件名称不能为空");
        }
        String content = null;
        try {
            content = GetFileContentUtil.buffedInput(multipartFile.getInputStream());
        } catch (IOException e) {
            throw new CustomException("文件读取异常");
        }
        SysFiles sysFiles = new SysFiles();
        sysFiles.setContent(content);
        sysFiles.setDelFlag("0");
        sysFiles.setRemark(fileVo.getRemark());
        sysFiles.setFileId(projId);
        sysFiles.setRemark(sysFiles.getRemark());
        sysFiles.setFileName(originalFilename);
        sysFiles.setCreateBy(fileVo.getCreateBy());
        sysFiles.setCreateTime(new Date());
        sysFiles.setUpdateTime(new Date());
        Integer insert = sysFileMapper.insert(sysFiles);
        if (insert == 0) {
            throw new CustomException("上传文件失败");
        }
    }

    @Override
    public void deleteFile(FileVo fileVo) {
        Integer[] fileIds = fileVo.getFileIds();
        if (fileIds.length <= 0) {
            throw new CustomException("接收文件id数组为空");
        }
        Integer integer = sysFileMapper.deleteFiles(fileIds);
        if (integer <= 0) {
            throw new CustomException("删除失败");
        }
    }

    @Override
    public List<SysFiles> queryAll(FileVo fileVo) {
        SysFiles sysFiles = new SysFiles();
        sysFiles.setCreateBy(fileVo.getCreateBy());
        sysFiles.setFileId(fileVo.getFileId());
        sysFiles.setProjId(fileVo.getProjId());
        return sysFileMapper.queryAll(sysFiles);
    }

    @Override
    public void auditFile(FileVo fileVo) {
        Integer fileId = fileVo.getFileId();
        if (fileId <= 0) {
            throw new CustomException("文件id不能为空");
        }
        String state = fileVo.getState();
        if (StringUtils.isEmpty(state)) {
            throw new CustomException("审核状态不能为空");
        }
        if (!state.equals("0") && !state.equals("2")) {
            throw new CustomException("审核文件状态不正确");
        }
        SysFiles sysFiles = new SysFiles();
        sysFiles.setDelFlag(state);
        sysFiles.setFileId(fileId);
        sysFiles.setUpdateTime(new Date());
        sysFileMapper.auditFile(sysFiles);
    }

}
